﻿using System;
using System.Windows.Forms;

namespace LM.Tela.Desktop
{
    using LM.InterfacesFabricas.Excecoes;
    using System.Linq;
    using System.Threading;

    public partial class frmPrincipal : Form
    {
        public frmPrincipal()
        {
            InitializeComponent();
        }

        static frmPrincipal()
        {
            AppDomain.CurrentDomain.UnhandledException += TrateExcecoesNaoTratadas;
            Application.ThreadException += ApplicationOnThreadException;
        }

        private static void ApplicationOnThreadException(object sender, ThreadExceptionEventArgs threadExceptionEventArgs)
        {
            TrateExcecao(threadExceptionEventArgs.Exception);
        }

        private static void TrateExcecoesNaoTratadas(object sender, UnhandledExceptionEventArgs unhandledExceptionEventArgs)
        {
            TrateExcecao(unhandledExceptionEventArgs.ExceptionObject as Exception);
        }

        private static void TrateExcecao(Exception ex)
        {
            var inconsistencia = ex as InconsistenciasException;

            if (inconsistencia != null)
            {
                MessageBox.Show(
                    string.Format(
                        "Inconsistências encontradas:\r\n\r\n{0}",
                        string.Join("\r\n", inconsistencia.Inconsistencias)),
                    "Inconsistência! - Não foi possível realizar a operação.",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Exclamation);

            }
            else
            {
                MessageBox.Show(
                   ex.ToString(),
                   "Erro!",
                   MessageBoxButtons.OK,
                   MessageBoxIcon.Error);
            }
        }
        public void CrieNovoFormulario<T>() where T : Form
        {
            var frm = MdiChildren.FirstOrDefault(f => f.Name == typeof(T).Name);

            if (frm == null)
            {
                var novoForm = Activator.CreateInstance(typeof(T)) as Form;
                novoForm.MdiParent = this;

                novoForm.Show();
            }
            else
            {
                frm.Activate();
            }
        }

        private void marcaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CrieNovoFormulario<frmMarca>();
        }

        private void vagaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CrieNovoFormulario<frmVaga>();
        }

        private void dependenteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CrieNovoFormulario<frmDependente>();
        }

        private void clienteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CrieNovoFormulario<frmCliente>();
        }

        private void garagemToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CrieNovoFormulario<frmGaragem>();
        }

        private void veículoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CrieNovoFormulario<frmVeiculo>();
        }

        private void modeloToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CrieNovoFormulario<frmModelo>();
        }

        private void operadorToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CrieNovoFormulario<frmOperador>();
        }

        private void relatórioDeLocaçãoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CrieNovoFormulario<frmRelatoriosLocacao>();
        }

        private void tabelaDePreçoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CrieNovoFormulario<frmTabelaPreco>();
        }

        private void sairToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Deseja Sair do Sistema?", "Confirmação", 
                MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)

                this.Close();
            {
                
            }
        }

        private void locaçãoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CrieNovoFormulario<frmLocacao>();
        }
    }
}
